package cn.edu.nuc.onlinestore.frame;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.server.TCPClient;
import cn.edu.nuc.onlinestore.vo.Cart;
import cn.edu.nuc.onlinestore.vo.Massage;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserGoods extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6285952423541502980L;
	private JPanel contentPane;
	private JTextField textField;
	private JFrame frame;
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserGoods frame = new UserGoods();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 * @param good 
	 * @param cart 
	 * @param tcp 
	 */
	public UserGoods(Goods good, Cart cart, TCPClient tcp) {
		this.frame=this;
		setTitle("商品详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("商品名称:");
		label.setBounds(23, 57, 66, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(132, 100, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 141, 44, 46);
		contentPane.add(label_4);
		
		JButton button = new JButton("加入购物车");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			}
		});
		button.setBounds(175, 243, 126, 23);
		contentPane.add(button);
		
		JLabel goodsname = new JLabel(good.getGoodsName());
		goodsname.setBounds(99, 57, 162, 15);
		contentPane.add(goodsname);
		
		JLabel price = new JLabel(String.valueOf(good.getPrice()));
		price.setBounds(99, 100, 54, 15);
		contentPane.add(price);
		
		JLabel jianjie = new JLabel(good.getJianjie());
		jianjie.setBounds(99, 141, 251, 46);
		contentPane.add(jianjie);
		
		textField = new JTextField();//获取购买数量
		textField.setText("1");
		textField.setBounds(99, 244, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("购买数量:");
		lblNewLabel_2.setBounds(35, 247, 71, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel 库存 = new JLabel("库存: "+good.getNum()+"件");
		库存.setBounds(99, 273, 135, 15);
		contentPane.add(库存);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				tcp.sendOptmessage(new Massage(Integer.parseInt(textField.getText()),good.getGoodsName(),"add",cart));
				if(tcp.getResult().equals("true")){
					JOptionPane.showMessageDialog(null, tcp.getmsg());
					((UserGoods)e.getSource()).setVisible(false);
				}else{
					JOptionPane.showMessageDialog(null, tcp.getmsg());
				}
			}
		});
	}
}
